/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.cux.workflow.controller;

import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.redragon.framework.springmvc.ControllerSupport;
import org.apache.commons.lang.StringUtils;
import org.hibernate.exception.ConstraintViolationException;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.JsonDataUtil;
import org.redragon.framework.util.JsonUtil;

import com.erp.cux.flexfield.dao.model.CuxFlexFieldHead;
import com.erp.cux.workflow.dao.data.DataBox;
import com.erp.cux.workflow.dao.model.CuxWorkflowConfHead;
import com.erp.cux.workflow.dao.model.CuxWorkflowConfHeadCO;
import com.erp.cux.workflow.service.CuxWorkflowConfHeadService;
import com.erp.cux.workflow.service.CuxWorkflowConfLineService;
import com.erp.dataset.service.DatasetCommonService;
import com.erp.dataset.service.SysDatasetService;

@Controller
@RequestMapping("/web/cuxWorkflowConfHead")
public class CuxWorkflowConfHeadWebController extends ControllerSupport{
    
    //日志处理
    private Logger logger = LoggerFactory.getLogger(CuxWorkflowConfHeadWebController.class);
    
    //服务层注入
    @Autowired
    private CuxWorkflowConfHeadService cuxWorkflowConfHeadService;
    @Autowired
    private CuxWorkflowConfLineService cuxWorkflowConfLineService;
    @Autowired
    private DatasetCommonService datasetCommonService;
    @Autowired
    private SysDatasetService sysDatasetService;
    
    @Override
    public String getExceptionRedirectUrl() {
        return this.getFormatRedirectUrl("getCuxWorkflowConfHeadList");
    }
    
    
    
    /**
     * 
     * @description 查询数据列表
     * @date 2021-02-27 17:15:38
     * @author 
     * @param pages
     * @param cuxWorkflowConfHeadCO
     * @param model
     * @return String
     *
     */
    @RequestMapping("getCuxWorkflowConfHeadList")
    public String getCuxWorkflowConfHeadList(Pages pages, CuxWorkflowConfHeadCO cuxWorkflowConfHeadCO, Model model) {
        //分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        
        //分页查询数据
        List<CuxWorkflowConfHead> cuxWorkflowConfHeadList = this.cuxWorkflowConfHeadService.getDataObjects(pages, cuxWorkflowConfHeadCO);
        
        //页面属性设置
        model.addAttribute("cuxWorkflowConfHeadList", cuxWorkflowConfHeadList);
        model.addAttribute("pages", pages);
        
        return "basic.jsp?content=cuxWorkflow/workflowConfList";
    }
    
    
    
    /**
     * 
     * @description 查询单条数据
     * @date 2021-02-27 17:15:38
     * @author 
     * @param cuxWorkflowConfHead
     * @param model
     * @return String
     *
     */
    @RequestMapping("getCuxWorkflowConfHead")
    public String getCuxWorkflowConfHead(CuxWorkflowConfHead cuxWorkflowConfHead, Model model) {
        //查询要编辑的数据
        if(cuxWorkflowConfHead!=null&&StringUtils.isNotBlank(cuxWorkflowConfHead.getConfHeadCode())) {
            CuxWorkflowConfHead cuxWorkflowConfHeadTemp = this.cuxWorkflowConfHeadService.getDataObject(cuxWorkflowConfHead.getConfHeadCode());
            if(cuxWorkflowConfHeadTemp!=null) {
                cuxWorkflowConfHead = cuxWorkflowConfHeadTemp;
            }
        }
        
        //业务类型
        Map workflowBusinessTypeMap = DataBox.getWorkflowBusinessType();
        
        //页面属性设置
        model.addAttribute("cuxWorkflowConfHead", cuxWorkflowConfHead);
        model.addAttribute("workflowBusinessTypeMap", workflowBusinessTypeMap);
        
        return "basic.jsp?content=cuxWorkflow/workflowConfEdit";
    }
    
    
    
    /**
     * 
     * @description 编辑数据
     * @date 2021-02-27 17:15:38
     * @author 
     * @param cuxWorkflowConfHead
     * @param bindingResult
     * @param model
     * @return String
     *
     */
    @RequestMapping("editCuxWorkflowConfHead")
    public String editCuxWorkflowConfHead(@Valid CuxWorkflowConfHead cuxWorkflowConfHead, BindingResult bindingResult, Model model, RedirectAttributes attr) {
        //参数校验
        Map<String, String> errorMap = this.validateParameters(bindingResult, model);
        if(errorMap.size()>0) {
            return "forward:getCuxWorkflowConfHead";
        }
        
        try {
            //对当前编辑的对象初始化默认的字段
            
            //保存编辑的数据
            this.cuxWorkflowConfHeadService.insertOrUpdateDataObject(cuxWorkflowConfHead);
            //提示信息
            attr.addFlashAttribute("hint", "success");
            attr.addAttribute("confHeadCode", cuxWorkflowConfHead.getConfHeadCode());
        }catch(Exception e){
            if(e.getCause().getClass()==ConstraintViolationException.class) {
                //提示信息
                attr.addFlashAttribute("hint", "hint");
                attr.addFlashAttribute("alertMessage", "工作流编码已存在，请重新输入");
                return "redirect:getCuxWorkflowConfHead";
            }else {
                throw e;
            }
        }
        
        return "redirect:getCuxWorkflowConfHead";
    }
    
    
    
    /**
     * 
     * @description 删除数据
     * @date 2021-02-27 17:15:38
     * @author 
     * @param cuxWorkflowConfHead
     * @return String
     *
     */
    @RequestMapping("deleteCuxWorkflowConfHead")
    public String deleteCuxWorkflowConfHead(CuxWorkflowConfHead cuxWorkflowConfHead, RedirectAttributes attr) {
        //删除数据前验证数据
        if(cuxWorkflowConfHead!=null&&cuxWorkflowConfHead.getConfHeadId()!=null&&StringUtils.isNotBlank(cuxWorkflowConfHead.getConfHeadCode())) {
            int count = this.cuxWorkflowConfLineService.getCuxWorkflowConfLineCountByHeadCode(cuxWorkflowConfHead.getConfHeadCode());
            
            if(count==0) {
                //删除数据
                this.cuxWorkflowConfHeadService.deleteDataObject(cuxWorkflowConfHead);
                //提示信息
                attr.addFlashAttribute("hint", "success");
            }else {
                //提示信息
                attr.addFlashAttribute("hint", "hint");
                attr.addFlashAttribute("alertMessage", "已配置工作流节点，无法删除");
            }
        }
        
        return "redirect:getCuxWorkflowConfHeadList";
    }
}
